package com.test.suan_fa.demo2;

import com.test.rabbitmq.demo.helloworld.P;

/**
 * @author weizhang240
 * @date 2020/6/18 9:57
 * @desc 给定一个值，从一个有序数组（从小到大排列） 找到其所在下标
 */
public class TwoSearch {

    public static int search(int[] arr, int target){
        int res = -1;
        int left = 0,right = arr.length-1;
        while (left <= right){
            int mid = (left + right) /2;
            if (arr[mid] > target) {
                right = mid - 1;
            }else if (arr[mid] < target){
                left = mid + 1;
            }else {
                return mid;
            }
        }
        return res;
    }

    public static void main(String[] args) {
        int[] arr = {3,4,6,10,27,48,95};
        System.out.println(search(arr, 788));
    }
}